package com.gushenge.core.impls

import com.gushenge.core.HttpQuery
import com.gushenge.core.beans.Codes
import com.gushenge.core.beans.Game
import com.gushenge.core.beans.Text
import com.gushenge.core.dao.GlobalConsts
import com.gushenge.core.dao.MMKVConsts
import com.gushenge.core.listeners.ResultListener
import com.gushenge.core.models.SearchModel
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class SearchImpl:SearchModel {
    override fun search(keyword: String, listener: ResultListener) {
        HttpQuery().search(GlobalConsts.SEARCH, keyword, MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Codes<Game>?> {
            override fun onFailure(call: Call<Codes<Game>?>, t: Throwable) {
//                listener.error(t.message?:"查询失败,请稍后重试")
            }

            override fun onResponse(call: Call<Codes<Game>?>, response: Response<Codes<Game>?>) {
                response.body()?.let {
                    when(it.code){
                        1 -> listener.success(it.data)
                        else -> listener.error(it.message)
                    }
                }
                response.body()?:listener.error("查询失败,请稍后重试")
            }
        })
    }

    override fun hot(listener: ResultListener) {
        HttpQuery().searchHot(GlobalConsts.SEARCH_HOT,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Codes<Text>?> {
            override fun onFailure(call: Call<Codes<Text>?>, t: Throwable) {
                listener.error()
            }

            override fun onResponse(call: Call<Codes<Text>?>, response: Response<Codes<Text>?>) {
                response.body()?.let {
                    when(it.code){
                        1->listener.success(it.data)
                        else-> listener.error()
                    }
                }
                response.body()?:listener.error()
            }
        })
    }
}